/**
 * PayBill
 * Copyright ? 2015 - 2017 
 * All rights reserved.
 * -----------------------------------
 * 2018-07-09  Created
 */
package com.lzy.cloud.payment.dtos.payment;

import com.lzy.cloud.payment.constants.CommonConstants;
import com.lzy.cloud.payment.dtos.pay.AlipayPayDTO;
import com.lzy.cloud.payment.dtos.pay.BasePayDTO;
import com.lzy.cloud.payment.dtos.pay.WeixinPayDTO;
import com.lzy.cloud.payment.entity.PayRefundInfo;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Description: PayBill
 * Author: mybatis-generator
 * Version: 3.0.0 
 * Create Date Time: 2018-07-09
 * Update Date Time: 
 * Add for lzy-cloud-service ;
 */

@Data
public class PayBillDto implements Serializable {
    private static final long serialVersionUID = -4182276618149782124L;
    /**
     * 自增ID
     */
    private Long id;

    /**
     * 用户ID
     */
    private Long memberId;

    /**
     * 关联本地ID  外键  充值提现支付表中的id
     */
    private String orderId;

    /**
     * 业务类型业务类型(0-支付)
     */
    private Integer type;

    /**
     * 角色(1:普通用户 2:商家)
     */
    private Integer role;

    /**
     * 描述
     */
    private String demo;

    /**
     * 金额
     */
    private BigDecimal amount;

    /**
     * 冲正标识(0:正常 1:已冲正)
     */
    private Integer correctFlag;

    /**
     * 创建时间
     */
    private String createTime;

    //支付宝
    public static Map<String, Object> AlipayPayInfo(AlipayPayDTO apDTO) {
        Map<String, Object> map = new HashMap<String, Object>();
        PayBillDto pbd = new PayBillDto();
        pbd.setMemberId(apDTO.getUserId());
        pbd.setAmount(BigDecimal.valueOf(Double.parseDouble(apDTO.getTotal_amount())));
        pbd.setCorrectFlag(CommonConstants.CorrectFlag.CORRECT_FLAG.getIndex());
        pbd.setDemo("支付方式:" + CommonConstants.PayWay.alipay.getName());
        pbd.setType(apDTO.getType());
        map.put("payBillDto", pbd);
        return map;
    }

    //微信
    public static Map<String, Object> WeixinPayInfo(WeixinPayDTO wxDTO) {
        Map<String, Object> map = new HashMap<String, Object>();
        //保存支付账单信息
        PayBillDto pbd = new PayBillDto();
        pbd.setMemberId(wxDTO.getUserId());
        pbd.setType(CommonConstants.BillType.pay.getIndex());
        pbd.setAmount(BigDecimal.valueOf(Double.parseDouble(wxDTO.getTotal_fee()) / 100));
        pbd.setCorrectFlag(CommonConstants.CorrectFlag.CORRECT_FLAG.getIndex());
        pbd.setDemo("支付方式:" + CommonConstants.PayWay.weixin.getName());
        pbd.setType(wxDTO.getType());
        map.put("payBillDto", pbd);
        return map;
    }

    /**
     * 在线支付/龙珠充值
     *
     * @param basePayDTO
     * @return
     */
    public static Map<String, Object> pay(BasePayDTO basePayDTO) {
        Map<String, Object> result = new HashMap<String, Object>();
        switch (basePayDTO.getWay()) {
            case 0:
                AlipayPayDTO apDTO = (AlipayPayDTO) basePayDTO;
                result = AlipayPayInfo(apDTO);
                break;
            case 1:
                WeixinPayDTO wxDTO = (WeixinPayDTO) basePayDTO;
                result = WeixinPayInfo(wxDTO);
                break;
            default:
                break;
        }
        return result;
    }

    /**
     * 退款
     *
     * @param payRefundInfo
     * @return
     */
    public static PayBillDto convert(PayRefundInfo payRefundInfo) {
        PayBillDto payBillDO = new PayBillDto();
        payBillDO.setMemberId(payRefundInfo.getMemberId());   //用户ID
        payBillDO.setAmount(payRefundInfo.getRefundAmount().negate());   //退款金额
        payBillDO.setCorrectFlag(CommonConstants.CorrectFlag.CORRECT_FLAG.getIndex());  //冲正标识
        payBillDO.setOrderId(payRefundInfo.getId().toString());     //退款订单号编号
        payBillDO.setType(CommonConstants.BillType.refund.getIndex());   //退款
        payBillDO.setDemo("原路返回");
        return payBillDO;
    }

}